package com.wyc.controller;

import com.wyc.pojo.AdminAnnMg;
import com.wyc.pojo.SuperAdminAnnMg;
import com.wyc.service.SuperAdminAnnMgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SuperAdminAnnMgController
 * @Author 王韫琛
 * @Date 2021/1/27 3:24
 * @Version 1.0
 */
@Controller
public class SuperAdminAnnMgController {
    @Autowired
    private SuperAdminAnnMgService superAdminAnnMgService;
    //跳转到注册公告页面
    @RequestMapping("/super_Admin_Ann")
    public String super_Admin_Ann(){
        return "/view/superAdmin_jsp/super_Admin_Announcement";
    }
    //跳转到公告维护页面
    @RequestMapping("/super_Admin_Main_Bull")
    public  String super_Admin_Main_Bull(){
        return "/view/superAdmin_jsp/super_Admin_Maintenance_Bulletin";
    }
    @RequestMapping("/addAnnMg")
    @ResponseBody
    public String addAnnMg(SuperAdminAnnMg superAdminAnnMg){
        superAdminAnnMg.setAdd_time(Timestamp.from(Instant.now()).toString());
        Map<String, Object> map = new HashMap<>();
        if (superAdminAnnMgService.addAnnMg(superAdminAnnMg)>0){
            map.put("success",true);
            map.put("message","添加成功");
        }else {
            map.put("success",false);
            map.put("message","修改失败");
        }
        return cn.hutool.json.JSONUtil.toJsonStr(map);
    }
    //超级管理员查询公告信息分页显示
    @ResponseBody
    @RequestMapping("/superAdminFindPage")
    public String superAdminFindPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                     @RequestParam(defaultValue = "10") Integer pageSize
                                    ){
        // @RequestParam(defaultValue = "超级管理员")String parde
        String parde="超级管理员";
        System.out.println(superAdminAnnMgService.getTotal(parde));
        Map<String,Object> map = new HashMap<>();
        //调用service得到分页对象
        List<SuperAdminAnnMg> pageInfo = superAdminAnnMgService.findPage(pageNum,pageSize,parde);
        System.out.println(pageInfo);
        map.put("code",0);
        map.put("msg","");
        map.put("count",superAdminAnnMgService.getTotal(parde));
        map.put("data",pageInfo);//数据列表
        return cn.hutool.json.JSONUtil.toJsonStr(map);
    }
    //超级管理员删除公告信息
    @RequestMapping("/superAdminDeleteById")
    @ResponseBody
    public String superDeleteById(Integer id){
       Map<String,Object> map = new HashMap<>();
        if (superAdminAnnMgService.deleteById(id) <= 0) {
            map.put("success",false);
            map.put("message","删除失败");
        } else {
            map.put("success",true);
            map.put("message","删除成功");
        }
        return cn.hutool.json.JSONUtil.toJsonStr(map);
    }
    @RequestMapping("/superAdminUpdate")
    @ResponseBody
    //超级管理员更新公告信息
    public String superAdminUpdate(SuperAdminAnnMg superAdminAnnMg){
        superAdminAnnMg.setAdd_time(Timestamp.from(Instant.now()).toString());
        Map<String, Object> map = new HashMap<>();
        if (superAdminAnnMgService.updateById(superAdminAnnMg)>0){
            map.put("success",true);
            map.put("message","修改成功");
        }else {
            map.put("success",false);
            map.put("message","修改失败");
        }
        return cn.hutool.json.JSONUtil.toJsonStr(map);
    }
    //根据作者搜索公告分页显示
    @ResponseBody
    @RequestMapping("/findPageByAuthor")
    public String findPageByAuthor(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,String author){
        String grade = "普通管理员";
        Map<String,Object> map = new HashMap<>();
        List<AdminAnnMg> pageInfo = superAdminAnnMgService.findPageByAuthor(pageNum, pageSize, author,grade);
        map.put("code",0);
        map.put("msg","");
        map.put("count",superAdminAnnMgService.getTotalByAuthor(author,grade));
        map.put("data",pageInfo);//数据列表
        return cn.hutool.json.JSONUtil.toJsonStr(map);
    }
    //超级管理员跳转到迁出申请管理界面
    @RequestMapping("/moveOutApplicationManagement")
    public String moveOutApplicationManagement(){
        return "/view/superAdmin_jsp/moveOutApplicationManagement";
    }


}
